﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace GraphicLib
{
    public class FileUtils
    {
        /// <summary>
        /// 移动源文件夹下的文件到目录文件夹
        /// </summary>
        /// <param name="sourceDir"></param>
        /// <param name="destDir"></param>
        public static void MoveFiles(string sourceDir, string destDir)
        {
            try
            {
                foreach (string filePath in Directory.GetFiles(sourceDir))
                {
                    try
                    {
                        File.Move(filePath, destDir + @"\" + Guid.NewGuid().ToString() + GetExtension(filePath));
                    }
                    catch (Exception ex)
                    {
                        File.AppendAllText(Sample.LOG_PATH, ex.Message);
                    }
                }
            }
            catch (Exception ex)
            {
                File.AppendAllText(Sample.LOG_PATH, ex.Message);
            }
        }

        /// <summary>
        /// 删除文件夹下的文件
        /// </summary>
        /// <param name="dir"></param>
        public static void DeleteFiles(string dir)
        {
            try
            {
                foreach (string filePath in Directory.GetFiles(dir))
                {
                    try
                    {
                        File.Delete(filePath);
                    }
                    catch (Exception ex)
                    {
                        File.AppendAllText(Sample.LOG_PATH, ex.Message);
                    }
                }
            }
            catch (Exception ex)
            {
                File.AppendAllText(Sample.LOG_PATH, ex.Message);
            }
        }

        /// <summary>
        /// 根据文件路径得到文件名
        /// </summary>
        /// <param name="filepath"></param>
        /// <returns></returns>
        public static string GetFileName(string filePath)
        {
            return filePath.Substring(filePath.LastIndexOf("\\") + 1);
        }

        /// <summary>
        /// 得到文件扩展名
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static string GetExtension(string filePath)
        {
            return filePath.Substring(filePath.LastIndexOf("."));
        }
    }
}
